package com.msb.serviceprice.service;

import com.msb.internalcommon.constant.CommonStatusEnum;
import com.msb.internalcommon.dto.PriceRule;
import com.msb.internalcommon.dto.ResponseResult;
import com.msb.internalcommon.request.ForecastPriceDTO;
import com.msb.internalcommon.response.DirectionResponse;
import com.msb.internalcommon.response.ForecastPriceResponse;
import com.msb.internalcommon.util.BigDecimalUtils;
import com.msb.serviceprice.mapper.PriceRuleMapper;
import com.msb.serviceprice.remote.ServiceMapClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: WuWen
 * @Date: 2023/8/18 - 08 - 18 - 20:51
 * @Description: com.msb.serviceprice.service
 * @version: 1.0
 */
@Service
@Slf4j
public class ForecastPriceService {
    @Autowired
    ServiceMapClient serviceMapClient;

    @Autowired
    PriceRuleMapper priceRuleMapper;

    public ResponseResult forecastPrice(String depLongitude, String depLatitude, String destLongitude, String destLatitude) {

        log.info("起点的经度是：" + depLongitude);
        log.info("起点的纬度是：" + depLatitude);
        log.info("终点的经度是：" + destLongitude);
        log.info("终点的纬度是：" + destLatitude);

        log.info("调用地图服务，查询距离和时长");
        ForecastPriceDTO forecastPriceDTO = new ForecastPriceDTO();
        forecastPriceDTO.setDepLongitude(depLongitude);
        forecastPriceDTO.setDepLatitude(depLatitude);
        forecastPriceDTO.setDestLongitude(destLongitude);
        forecastPriceDTO.setDestLatitude(destLatitude);

        ResponseResult<DirectionResponse> direction = serviceMapClient.direction(forecastPriceDTO);
        Integer distance = direction.getData().getDistance();
        Integer duration = direction.getData().getDuration();
        log.info("距离：" + distance + "，时间：" + duration);

        log.info("读取计价规则");
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("city_code", "110000");
        queryMap.put("vehicle_type", "1");
        List<PriceRule> priceRules = priceRuleMapper.selectByMap(queryMap);

        if (priceRules.size() == 0) {
            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EMPTY.getCode(), CommonStatusEnum.PRICE_RULE_EMPTY.getValue());

        }

        PriceRule priceRule = priceRules.get(0);

        log.info("根据计价规则、距离、时长，计算价格");
        Double price = getPrice(distance, duration, priceRule);


        ForecastPriceResponse forecastPriceResponse = new ForecastPriceResponse();
        forecastPriceResponse.setPrice(price);

        return ResponseResult.success(forecastPriceResponse);
    }

    /**
     * 根据计价规则、距离、时长，计算价格
     * @param distance
     * @param duration
     * @param priceRule
     * @return
     */
    private Double getPrice(Integer distance,Integer duration,PriceRule priceRule){
        Double price = 0.0;
        // 起步价
        Double startFare = priceRule.getStartFare();
        price = BigDecimalUtils.add(price,startFare);
        // 里程费
        // 里程 km
        double distanceMile = BigDecimalUtils.divide(distance, 1000);
        Integer startMile = priceRule.getStartMile();
        double distanceSubtract= BigDecimalUtils.subtract(distanceMile, startMile);
        double mile= distanceSubtract<0?0:distanceSubtract;
        Double pricePerMile = priceRule.getUmitPricePerMile();
        double mileFare = BigDecimalUtils.multiply(mile,pricePerMile);
        price = BigDecimalUtils.add(price,mileFare);

        //时长费
        Double pricePerMinute = priceRule.getUmitPricePerMinute();
        double durationMinute = BigDecimalUtils.divide(duration, 60);
        double timeFare = BigDecimalUtils.multiply(pricePerMinute, durationMinute);
        price = BigDecimalUtils.add(price,timeFare);
        BigDecimal priceBigDecimal = new BigDecimal(price);
        priceBigDecimal = priceBigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);

        return priceBigDecimal.doubleValue();
    }
}
